<?php

namespace App\Admin\Controllers;

use App\Models\JxtSpecialist;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SpecialistController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '专家管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new JxtSpecialist());

        $grid->column('id', __('Id'));
        $grid->column('name', __('姓名'));
        $grid->column('position', __('职称'));
        $grid->column('head_image', __('图片'))->display(function ($value) {
            if (!empty($value)) {
                return "<img width='100' src='" . config('filesystems.disks.admin.url') . '/' . $value . "'/>";
            }
        });
        $grid->column('status', __('是否开放'))->display(function ($value) {
            $class = $value == JxtSpecialist::STATUS_SHOW ? 'label-success' : 'label-danger';
            return "<span class='label " . $class . "'>" . JxtSpecialist::$status[$value] . "</span>";
        });
        $grid->column('sort', __('排序'));
        $grid->column('created_at', __('创建日期'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(JxtSpecialist::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('姓名'));
        $show->field('position', __('职称'));
        $show->field('head_image', __('图片'))->setEscape(false)->as(function ($value) {
            return "<img width='100' src='" . config('filesystems.disks.admin.url') . '/' . $value . "'/>";
        });
        $show->field('introduce', __('简介'));
        $show->field('content', __('内容'))->unescape();
        $show->field('status', __('是否显示'))->as(function ($value) {
            return JxtSpecialist::$status[$value];
        });
        $show->field('sort', __('排序'));
        $show->field('created_at', __('创建日期'));
        $show->field('updated_at', __('更新日期'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new JxtSpecialist());

        $form->text('name', __('姓名'))->rules('required', [
            'required' => '请输入姓名！'
        ]);
        $form->text('position', __('职称'))->rules('required', [
            'required' => '请输入职称！'
        ]);
        $form->text('introduce', __('描述'))->help('一小段文字性介绍');
        $form->image('head_image', __('图片'))->uniqueName()->move('specialist/images/' . date('Ymd'))->required();
        $form->editor('content', __('内容'))->rules('required', [
            'required' => '请输入内容！'
        ]);
        $form->number('sort', __('Sort'));

        return $form;
    }
}
